AppleScript is a language intended to automatize tasks. The programs written with AppleScripts are called AppleScript scripts, or, when allowed by the context, scripts. Most often, a script will replace tenths (or millions) of manual operations by one operation such as double-clicking an icon, clicking a button, or selecting an item in a pull-down menu.
AppleScript has its own set of commands, which allows some basic manipulations of quantities such as numbers, strings, data, lists, records. This basic set can be freely extended by the mechanism of the scripting additions ("Compléments de pilotage" en français). A scripting addition (or "osax") is a file which, when installed in the proper folder, will add some functionalities to AppleScript.
Adding a functionality to AppleScript actually means adding some new terms to the AppleScript's standard terminology. These new terms are called the "dictionary" of the scripting addition. (Check the "File" menu for the item "Open dictionary…".). For instance, by installing the "Breakfast" scripting addition, you will add a "make coffee" verb to the AppleScript's terminology.
Some applications also have a dictionary, which extends still further the available terminology. AppleScript allows to send a message to some specific application : "tell application "Finder" to beep". Each application has its own dictionary, which presents what messages and what data you can send and receive to / from the application.
In applets and droplets, you fill find a tutorial about making those. Applets (for: small applications) are scripts which are launchable by double-click on their icon, while droplets are launched when files are dropped on their icon. Smile's own objects have scripts, allowing for a richer behavior. This aspect is presented below, through a simple - yet, useful - example.
Writing your first script for Smile
• Select "New text" ("File" menu). This opens a new text (white) window.
• While pressing down the command and the option keys ( ), click in the white area of the new window. This will open a new script (colored) window.
• Type the following lines in the new window (or copy or drag the text) :
----------------------------
property cookingDelay : 180 -- the cooking time, in seconds, for boiled eggs
property startingTime : "" -- the time cooking started
on prepare theWindow -- sent to the script when window opens
"Time remaining : " & cookingDelay & " seconds." to after text of theWindow
set want idle of theWindow to true -- make Smile call the "idle" handler
end reset
on idle theWindow -- sent after delay = the returned value (2.0) since last time
set theElapsed to (current date) - startingTime
if theElapsed < cookingDelay then
set last paragraph of theWindow to "Time remaining : " & (cookingDelay - theElapsed) & " seconds." -- update last line
return 2.0 -- time before next call to 'idle' handler
end if
set want idle of theWindow to false -- time elapsed, suspend "idle" calls
try
say "Eggs are ready"
on error
beep 3 -- code for "Eggs are ready"
end try
set last paragraph of theWindow to "Boiled eggs ready : " & (current date) & return
end idle
on DoHelp(theWindow)
try
set cookingDelay to 0 + AskUser("Cooking time (seconds) : ", "" & cookingDelay)
on error
end try
end DoHelp
----------------------------
• Read the script : the "prepare" handler is called automatically by Smile when the window is opened, it just "resets" the window. The "reset" handler launches the timing. The "idle" handler is called automatically by Smile if the property "want idle" of the window is set to "true". Here, it checks whether the specified delay is elapsed. When it is, it says something or beeps.
Note the way the possible failure of the "say" call (the Speech ability can be absent from the user's machine) is handled.
The "DoHelp" handler is called when the user presses the "Help" key. Here, pressing the "Help" key allows to set the cooking time. The "try" in the "DoHelp" handler is intended to manage the possible "User canceled" error.
• Select "Check syntax" ("Edit" menu) (the new colored window must be the active window). Correct any possible mistake.
• Select "Save", then "Close" ("File" menu).
• Click the new white window to make it the active window, then select "Save" ("File" menu). Choose the desktop as the location for the file, name it "Egg Timer".
• Usually, you will launch the timer by double-clicking the "Egg Timer" icon. But presently it is already opened, so launch the timing manually (by script, actually). Type in the "Egg timer" window the following line :
----------------------------
tell window 1 to reset(it)
----------------------------
Then press the Enter key (numeric pad). (On most machines, you can alternately type ctrl-C.)
• For testing purposes, you may want to shorten the delay. Press the "Help" key (or type -?). In the dialog, give a smaller value such as 30.
• Now, wait a few seconds, the window should soon, either say that the eggs are ready, or emit three beeps.
• Select "Save", then "Close" ("File" menu). Not only have you saved the text of the window. You have also saved its script, including the current values of its properties (the 30 seconds delay), in other terms, its settings.
Later, when you need to set some alarm, double-click the document in Finder, press the "Help" key to set the delay. Smile will tell you when the alarm goes off.